//
//  UITableView+Empty.m
//  CarFile
//
//  Created by 陈浩 on 2017/8/2.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "UITableView+Empty.h"
@interface UITableView()

@property (nonatomic,strong) UILabel *placeholderLabel; /**< 占位文字label */
@property (nonatomic ,strong) UIImageView *imgView;


@end
@implementation UITableView (Empty)
+(void)load
{
    //交换reloadData方法
    method_exchangeImplementations(class_getInstanceMethod(self, @selector(reloadData)), class_getInstanceMethod(self, @selector(lx_reloadData)));
    
    //交换insertSections方法
    method_exchangeImplementations(class_getInstanceMethod(self,@selector(insertSections:withRowAnimation:)),class_getInstanceMethod(self,@selector(lx_insertSections:withRowAnimation:)));
    
    //交换insertRowsAtIndexPaths方法
    method_exchangeImplementations(class_getInstanceMethod(self, @selector(insertRowsAtIndexPaths:withRowAnimation:)),  class_getInstanceMethod(self, @selector(lx_insertRowsAtIndexPaths:withRowAnimation:)));
    
    //交换deleteSections方法
    method_exchangeImplementations(class_getInstanceMethod(self,@selector(deleteSections:withRowAnimation:)),class_getInstanceMethod(self,@selector(lx_deleteSections:withRowAnimation:)));
    
    //交换deleteRowsAtIndexPaths方法
    method_exchangeImplementations(class_getInstanceMethod(self,@selector(deleteRowsAtIndexPaths:withRowAnimation:)),class_getInstanceMethod(self,@selector(lx_deleteRowsAtIndexPaths:withRowAnimation:)));
}

//reloadData
-(void)lx_reloadData
{
    [self lx_reloadData];
    
    [self reloadPlaceoholderLabel];
}

//insert
-(void)lx_insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
{
    [self lx_insertSections:sections withRowAnimation:animation];
    [self reloadPlaceoholderLabel];
}

-(void)lx_insertRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
{
    [self lx_insertRowsAtIndexPaths:indexPaths withRowAnimation:animation];
    [self reloadPlaceoholderLabel];
}

//delete
-(void)lx_deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation
{
    [self lx_deleteSections:sections withRowAnimation:animation];
    [self reloadPlaceoholderLabel];
}

-(void)lx_deleteRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
{
    [self lx_deleteRowsAtIndexPaths:indexPaths withRowAnimation:animation];
    [self reloadPlaceoholderLabel];
}

#pragma mark -- 重新设置数据
-(void)reloadPlaceoholderLabel
{
    [self.imgView removeFromSuperview];
    [self.placeholderLabel removeFromSuperview];
    
    //计算行数
    NSInteger rows = 0;
    for (int i = 0; i <  self.numberOfSections; i ++) {
        
        rows += [self numberOfRowsInSection:i];
    }
    
    //如果没有数据 或者字符串为空 就不显示
    if (rows > 0 || !self.placeholder|| !self.placeholderImage) {
        
        return;
    }
    
    //是否有偏移
    CGFloat height = self.contentInset.top;
    
    //判断是否有头
    if (self.tableHeaderView) {
        height += self.tableHeaderView.h;
    }
    
    if (height < self.h/2.0-100) {
        height = self.h/2.0-100;
    }else {
        height += 30;
    }
    
    
    self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake((self.w-80*ScaleX)/2, height, 80*ScaleX, 80*ScaleY)];
    self.imgView.image = [UIImage imageNamed:self.placeholderImage];
    [self addSubview:self.imgView];
    
    //显示提示内容的label
    self.placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(self.imgView.frame), self.w-40, 60*ScaleY)];
    self.placeholderLabel.numberOfLines = 3;
    self.placeholderLabel.text = self.placeholder;
    self.placeholderLabel.font = Font(14);
    self.placeholderLabel.textColor = [UIColor lightGrayColor];
    self.placeholderLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.placeholderLabel];
}

#pragma mark -- getter & setter
-(NSString *)placeholder
{
    return objc_getAssociatedObject(self, @selector(placeholder));
}

-(NSString *)placeholderImage{
    
    return objc_getAssociatedObject(self, @selector(placeholderImage));
}


-(void)setPlaceholder:(NSString *)placeholder
{
    objc_setAssociatedObject(self, @selector(placeholder), placeholder, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    [self reloadPlaceoholderLabel];
}

-(void)setPlaceholderImage:(NSString *)placeholderImage{
    
    objc_setAssociatedObject(self, @selector(placeholderImage), placeholderImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    [self reloadPlaceoholderLabel];
}

-(UIImageView *)imgView{
   return objc_getAssociatedObject(self, @selector(imgView));
}

-(UILabel *)placeholderLabel
{
    return objc_getAssociatedObject(self, @selector(placeholderLabel));
}

-(void)setImgView:(UIImageView *)imgView{
    
    objc_setAssociatedObject(self, @selector(imgView), imgView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(void)setPlaceholderLabel:(UILabel *)placeholderLabel
{
    objc_setAssociatedObject(self, @selector(placeholderLabel), placeholderLabel, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

